﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using MySql.Data;
using MySql.Data.MySqlClient;

namespace WebApplication1
{
    public partial class WebForm8 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.IsPostBack == false)
            {
                LoadProizvoditeli();
            }
        }

        protected void LoadProizvoditeli()
        {
            MySqlConnection konekcija = new MySqlConnection();
            konekcija.ConnectionString = ConfigurationManager.ConnectionStrings["MojaKonekcija"].ConnectionString;
            // konekcija.ConnectionString = "server=localhost;User Id=root;database=avtomobili;";

            MySqlCommand komanda = new MySqlCommand();
            komanda.Connection = konekcija;
            komanda.CommandText = "SELECT ImeProizvoditel FROM proizvoditeli";

            try
            {
                konekcija.Open();

                MySqlDataReader citac = komanda.ExecuteReader();
                while (citac.Read())
                {
                    ListItem element = new ListItem();
                    element.Text = citac["ImeProizvoditel"].ToString();
                    element.Value = citac["ImeProizvoditel"].ToString();
                    ProizvoditeliListBox.Items.Add(element);
                }
                citac.Close();
            }
            catch (Exception ex)
            {
                // Label2.Text = ex.Message;
            }
            finally
            {
                konekcija.Close();
            }
        }

        protected void LoadModeli(String Marka)
        {
            MySqlConnection konekcija = new MySqlConnection();
            konekcija.ConnectionString = ConfigurationManager.ConnectionStrings["MojaKonekcija"].ConnectionString;
            // konekcija.ConnectionString = "server=localhost;User Id=root;database=avtomobili;";

            MySqlCommand komanda = new MySqlCommand();
            komanda.Connection = konekcija;
            komanda.CommandText = "SELECT Model, Broj FROM modeli WHERE Proizvoditel='" + Marka + "'";

            try
            {
                konekcija.Open();

                MySqlDataReader citac = komanda.ExecuteReader();
                while (citac.Read())
                {
                    ListItem element = new ListItem();
                    element.Text = citac["Model"].ToString();
                    element.Value = citac["Broj"].ToString();
                    ModeliListBox.Items.Add(element);
                }
                citac.Close();
            }
            catch (Exception ex)
            {
                //   Label2.Text = ex.Message;
            }
            finally
            {
                konekcija.Close();
            }
        }

        protected void LoadModelInfo(String ID)
        {
            MySqlConnection konekcija = new MySqlConnection();
            konekcija.ConnectionString = ConfigurationManager.ConnectionStrings["MojaKonekcija"].ConnectionString;
            // konekcija.ConnectionString = "server=localhost;User Id=root;database=avtomobili;";

            MySqlCommand komanda = new MySqlCommand();
            komanda.Connection = konekcija;
            komanda.CommandText = "SELECT * FROM modeli WHERE Broj='" + ID + "'";

            try
            {
                konekcija.Open();

                MySqlDataReader citac = komanda.ExecuteReader();
                if (citac.Read())
                {
                    ModelBroj.Text = citac["Broj"].ToString();
                    ModelID.Text = citac["MarkaSifra"].ToString();
                    ModelProizvoditel.Text = citac["Proizvoditel"].ToString();
                    ModelModel.Text = citac["Model"].ToString();
                    ModelTip.Text = citac["Tip"].ToString();
                    ModelInfo.Text = citac["Informacii"].ToString();
                    ModelSlika.Text = citac["Slika"].ToString();
                    ModelWeb.Text = citac["WebModeli"].ToString();
                }
                citac.Close();
            }
            catch (Exception ex)
            {
                //  Label2.Text = ex.Message;
            }
            finally
            {
                konekcija.Close();
            }
        }

        protected void ProizvoditeliListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            ModeliListBox.Items.Clear();
            LoadModeli(ProizvoditeliListBox.SelectedValue);
        }

        protected void ModeliListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadModelInfo(ModeliListBox.SelectedValue);
        }

        protected void ButtonIzbrisi_Click(object sender, EventArgs e)
        {
            DataBase.IzbrisiModel(ModelBroj.Text);
            ModeliListBox.Items.Clear();
            LoadModeli(ProizvoditeliListBox.SelectedValue);
        }

        protected void ButtonPromeni_Click(object sender, EventArgs e)
        {
            int poz = ModeliListBox.SelectedIndex;
            DataBase.AzurirajModel(ModelBroj.Text, ModelID.Text, ModelProizvoditel.Text, ModelModel.Text, ModelTip.Text, ModelInfo.Text, ModelSlika.Text, ModelWeb.Text);
            ModeliListBox.Items.Clear();
            LoadModeli(ProizvoditeliListBox.SelectedValue);
            ModeliListBox.SelectedIndex = poz;
        }

        protected void ButtonVnesi_Click(object sender, EventArgs e)
        {
            DataBase.VnesiModel(ModelID.Text, ModelProizvoditel.Text, ModelModel.Text, ModelTip.Text, ModelInfo.Text, ModelSlika.Text, ModelWeb.Text);
            ModeliListBox.Items.Clear();
            LoadModeli(ProizvoditeliListBox.SelectedValue);
        }
    }
}
